﻿using System;
using System.Security;
using Codeplex;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;

namespace SKN
{
    /// <summary>
    /// Contains common method for Navigation
    /// </summary>
    public static class Navigation
    {
        /// <summary>
        /// Use this method to add a Quick Launch heading 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="nodeTitle"></param>
        /// <param name="nodeLink"></param>
        /// <param name="location"></param>
        /// <example>Navigation.AddQuickLaunchHeading(rootWeb,"Dashboard","/dashboard.aspx","last");</example>
        public static void AddQuickLaunchHeading(SPWeb web, string nodeTitle, string nodeLink, string location)
        {
            var nodeCollection = web.Navigation.QuickLaunch;
            var node = new SPNavigationNode(nodeTitle, nodeLink, true);

           
                switch (location.ToLower())
                {
                    case "last":
                        nodeCollection.AddAsLast(node);
                        break;
                    case "first":
                        nodeCollection.AddAsFirst(node);
                        break;
                    default:
                        nodeCollection.AddAsLast(node);
                        break;
                }
                web.Update();
            
            
        }

        /// <summary>
        /// Use this method to delete a Quick Launch heading and all its sub headings
        /// </summary>
       /// <param name="web"></param>
       /// <param name="title">Title of the heading to be deleted</param>
        /// <example>Navigation.DeleteQuickLaunchHeading(web,"Dashboard");</example>
        public static void DeleteQuickLaunchHeading(SPWeb web, string title)
        {
            SPNavigationNodeCollection nodeCollection = web.Navigation.QuickLaunch;

            foreach (SPNavigationNode node in nodeCollection)
            {
                if (string.Equals(node.Title, title, StringComparison.InvariantCultureIgnoreCase))
                {
                    nodeCollection.Delete(node);
                    break;
                }
            }

            web.Update();
        }

        /// <summary>
        /// Use this method to add a Top NavBar heading 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="nodeTitle"></param>
        /// <param name="nodeLink"></param>
        /// <param name="location"></param>
        /// <example>Navigation.AddTopNavHeading(rootWeb,"Dashboard","/dashboard.aspx","last");</example>
        public static void AddTopNavHeading(SPWeb web, string nodeTitle, string nodeLink, string location)
        {
                bool nodeExists = false;
                foreach (SPNavigationNode node in web.Navigation.TopNavigationBar)
                {
                    if (node.Title == nodeTitle && node.Url == nodeLink )
                    {
                        nodeExists = true;
                        break;
                    }
                }

                if (!nodeExists)
                {
                    SPNavigationNodeCollection nodeCollection = web.Navigation.TopNavigationBar;

                    var node = new SPNavigationNode(nodeTitle, nodeLink, true);

                    if (!web.Navigation.UseShared)
                    {
                        switch (location.ToLower())
                        {
                            case "last":
                                nodeCollection.AddAsLast(node);
                                break;
                            case "first":
                                nodeCollection.AddAsFirst(node);
                                break;
                            default:
                                nodeCollection.AddAsLast(node);
                                break;
                        }

                        web.Update();
                    }
                }
        }

        /// <summary>
        /// Use this method to delete a Top NavBar and all its sub headings
        /// </summary>
        /// <param name="web"></param>
        /// <param name="title"></param>
        /// <example>Navigation.DeleteTopNavHeading(web,"Dashboard");</example>
        public static void DeleteTopNavHeading(SPWeb web, string title)
        {
            SPNavigationNodeCollection nodeCollection = web.Navigation.TopNavigationBar ;

            foreach (SPNavigationNode node in nodeCollection)
            {
                if (string.Equals(node.Title, title, StringComparison.InvariantCultureIgnoreCase))
                {
                    nodeCollection.Delete(node);
                    break;
                }
            }

            web.Update();
        }

        /// <summary>
        /// Use this method to add a custom site map to web application
        /// </summary>
        /// <param name="webApp">WebApplication object to which the site belong</param>
        /// <param name="sitemapFilename">Name of the file to be updated</param>
        /// <example >Navigation.AddCustomSitemap(web.Site.WebApplication,"layouts.sitemap.client.xml");</example>
        public static void AddCustomSitemap(SPWebApplication webApp, string sitemapFilename)
        {
            var uls = new UpdateLayoutsSitemap(webApp);
            uls.AddSitemap(sitemapFilename);
            uls.SubmitJob();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="webApp"></param>
        public static void UpdateSiteMap(SPWebApplication webApp)
        {
            try
            {
                webApp.Farm.Services.GetValue<SPWebService>().ApplyApplicationContentToLocalServer();
            }
            catch (SecurityException)
            {
                

            }
        }
    }
}